package com.scheduling.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scheduling.order.entity.Order;
import com.scheduling.order.entity.dto.OrderDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 订单Mapper接口
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 分页查询订单详情（包含客户信息）
     */
    Page<OrderDTO> selectOrderDTOPage(Page<?> page,
                                      @Param("customerName") String customerName,
                                      @Param("status") String status,
                                      @Param("startDate") LocalDate startDate,
                                      @Param("endDate") LocalDate endDate);

    /**
     * 根据订单ID查询订单详情（包含客户信息）
     */
    OrderDTO selectOrderDTOById(@Param("orderId") String orderId);

    /**
     * 查询指定状态的订单详情列表（包含客户信息）
     */
    List<OrderDTO> selectOrderDTOsByStatus(@Param("status") String status);

    /**
     * 查询逾期订单详情（包含客户信息）
     */
    List<OrderDTO> selectOverdueOrderDTOs();

    /**
     * 分页查询订单基础信息
     */
    @Select("SELECT o.order_id, o.customer_id, o.due_date, o.status, o.create_time, o.update_time " +
            "FROM `order` o " +
            "WHERE (#{customerName} IS NULL OR EXISTS (SELECT 1 FROM customer c WHERE c.customer_id = o.customer_id " +
            "AND c.customer_name LIKE CONCAT('%', #{customerName}, '%'))) " +
            "AND (#{status} IS NULL OR o.status = #{status}) " +
            "AND (#{startDate} IS NULL OR o.due_date >= #{startDate}) " +
            "AND (#{endDate} IS NULL OR o.due_date <= #{endDate}) " +
            "ORDER BY o.create_time DESC")
    Page<Order> selectOrderPage(Page<?> page,
                                @Param("customerName") String customerName,
                                @Param("status") String status,
                                @Param("startDate") LocalDate startDate,
                                @Param("endDate") LocalDate endDate);

    /**
     * 根据订单ID查询订单基础信息
     */
    @Select("SELECT o.order_id, o.customer_id, o.due_date, o.status, o.create_time, o.update_time " +
            "FROM `order` o " +
            "WHERE o.order_id = #{orderId}")
    Order selectOrderDetailById(@Param("orderId") String orderId);

    /**
     * 查询指定状态的订单基础信息列表
     */
    @Select("SELECT o.order_id, o.customer_id, o.due_date, o.status, o.create_time, o.update_time " +
            "FROM `order` o " +
            "WHERE o.status = #{status} " +
            "ORDER BY o.due_date ASC")
    List<Order> selectOrdersByStatus(@Param("status") String status);

    /**
     * 查询逾期订单基础信息
     */
    @Select("SELECT o.order_id, o.customer_id, o.due_date, o.status, o.create_time, o.update_time " +
            "FROM `order` o " +
            "WHERE o.due_date < CURDATE() AND o.status != '生产完成' " +
            "ORDER BY o.due_date ASC")
    List<Order> selectOverdueOrders();
} 